package com.huayi.website.controller.manage;

import com.huayi.website.entity.PhotoEntity;
import com.huayi.website.entity.ProductEntity;
import com.huayi.website.request.GoodsAddVo;
import com.huayi.website.request.GoodsDeleteReq;
import com.huayi.website.service.PhotoService;
import com.huayi.website.utils.EnumResultVo;
import com.huayi.website.utils.PagingResponse;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manage")
@Controller
public class MPhotoController {
    @Autowired
    private PhotoService photoService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/photo_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {

        Integer pageIndex = 1, pageSize = 10;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }

        var result = photoService.getAllList(pageIndex,pageSize,null);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);

        model.addAttribute("list", result.getList());
        model.addAttribute("totalSize", result.getTotalSize());



        model.addAttribute("menu","photo_list");
        return "manage/photo_list";
    }
    /**
     * 添加
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/photo_add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, Model model) {


        model.addAttribute("photo",new PhotoEntity());
        model.addAttribute("menu","photo_list");
        return "manage/photo_add";
    }

    @RequestMapping(value = "/photo_edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, @RequestParam Integer id, Model model) {
        var photo = photoService.getPhotoById(id);
        model.addAttribute("photo",photo);

        model.addAttribute("menu","photo_list");
        return "manage/photo_add";
    }

    @ResponseBody
    @RequestMapping(value = "/photo_add_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");

        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)) id= "0";
        String title = request.getParameter("title");
        String desc = request.getParameter("desc");
        String sort = request.getParameter("sort");
        Integer companyId = Integer.parseInt(request.getParameter("companyId"));

        PhotoEntity photo = new PhotoEntity();
        photo.setId(Integer.parseInt(id));
        photo.setCompanyId(companyId);
        photo.setTitle(title);
        photo.setDesc(desc);
        photo.setSort(Integer.parseInt(sort));
        //图片
        String[] images = request.getParameterValues("image");
        if (images == null || images.length == 0 || StringUtils.isEmpty(images[0])) {
            //商品主图不能为空
            return new ResultVo<>(200, "至少上传一张图片");
        }
        for (var img:images) {
            if(StringUtils.isEmpty(img)==false)
                photo.getImages().add(img);
        }


        Integer photoId = photoService.editPhoto(photo);


        return new ResultVo<>(0, "SUCCESS");
    }


    /**
     * 删除
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/photo_delete", method = RequestMethod.POST)
    public ResultVo<Integer> delete(@RequestBody GoodsDeleteReq req) {
        if (req.getId() == null || req.getId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError.getIndex(), "参数错误，缺少id");

        photoService.deletePhoto(req.getId());

        return new ResultVo<>(EnumResultVo.SUCCESS);
    }

}
